using System;
using System.Collections;
using System.Globalization;
using UnityEngine;
using UnityEngine.Networking;
using Voodoo.Sauce.Common.Utils;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.CrossPromo.Mercury.Models;
using Voodoo.Sauce.Internal.CrossPromo.Mercury.Parameters;
using Voodoo.Sauce.Internal.Utils;
using Voodoo.Sauce.Privacy;

namespace Voodoo.Sauce.Internal.CrossPromo.Mercury
{
	public class MercuryAPI : MonoBehaviour
	{
		private const string TAG = "MercuryAPI";

		private static MercuryAPI _instance;

		private const string MERCURY_URL = "https://crosspromo.voodoo.io";

		private const string MERCURY_TOKEN = "Token 16aaae9ea829470dc2993d0afe865d1165230589";

		private static PrivacyCore _privacy => VoodooSauceCore.GetPrivacy();

		public static MercuryAPI Instance
		{
			get
			{
				if (!_instance)
				{
					GameObject obj = new GameObject("MercuryAPI");
					_instance = obj.AddComponent<MercuryAPI>();
					UnityEngine.Object.DontDestroyOnLoad(obj);
				}
				return _instance;
			}
		}

		public static void GetGameInfo(GetGameInfoParameters parameters, Action<MercuryRequestInfo> onSuccess, Action<MercuryRequestInfo> onFailure)
		{
			Instance.StartCoroutine(GetGameInfoCoroutine(parameters, onSuccess, onFailure));
		}

		private static IEnumerator GetGameInfoCoroutine(GetGameInfoParameters parameters, Action<MercuryRequestInfo> onSuccess, Action<MercuryRequestInfo> onFailure)
		{
			VoodooLog.LogDebug(Module.CROSS_PROMO, "MercuryAPI", "GetGameInfo with: " + parameters.ToJsonString());
			UnityWebRequest webRequest = CreateGameInfoRequest(parameters);
			VoodooLog.LogDebug(Module.CROSS_PROMO, "MercuryAPI", "URL Used: " + webRequest.url);
			webRequest.timeout = 5;
			float startTime = Time.time;
			yield return webRequest.SendWebRequest();
			float num = Time.time - startTime;
			bool flag = num >= (float)webRequest.timeout;
			MercuryRequestInfo mercuryRequestInfo = default(MercuryRequestInfo);
			mercuryRequestInfo.downloadTime = num;
			mercuryRequestInfo.hasTimeout = flag;
			mercuryRequestInfo.httpResponse = webRequest.responseCode.ToString();
			mercuryRequestInfo.gamesPromoted = "";
			MercuryRequestInfo obj = mercuryRequestInfo;
			if (flag)
			{
				VoodooLog.LogWarning(Module.CROSS_PROMO, "MercuryAPI", "Timeout while getting info: " + webRequest.error);
				onFailure?.Invoke(obj);
				yield break;
			}
			if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
			{
				VoodooLog.LogWarning(Module.CROSS_PROMO, "MercuryAPI", "Error while getting info: " + webRequest.error);
				onFailure?.Invoke(obj);
				yield break;
			}
			VoodooLog.LogDebug(Module.CROSS_PROMO, "MercuryAPI", "Success while getting info: " + webRequest.downloadHandler.text);
			if (!string.IsNullOrEmpty(webRequest.downloadHandler.text))
			{
				MercuryWaterfall waterfall = JsonUtility.FromJson<MercuryWaterfall>(webRequest.downloadHandler.text);
				obj.waterfall = waterfall;
				obj.data = webRequest.downloadHandler.text;
			}
			onSuccess?.Invoke(obj);
		}

		public static UnityWebRequest CreateGameInfoRequest(GetGameInfoParameters info)
		{
			return null;
		}

		public static GetGameInfoParameters CreateDefaultGameInfoParameters(string format, string gameList, string waterfallId, bool restrictedPrivacy)
		{
			string advertisingId = _privacy.GetAdvertisingId();
			AnalyticsSessionManager analyticsSessionManager = AnalyticsSessionManager.Instance();
			string osType = "ios";
			if (PlatformUtils.UNITY_ANDROID)
			{
				osType = "android";
			}
			GetGameInfoParameters result = default(GetGameInfoParameters);
			result.restrictedPrivacy = restrictedPrivacy;
			result.cpFormat = format;
			result.waterfallId = waterfallId;
			result.waterfallGameList = gameList;
			result.bundleId = Application.identifier;
			result.osType = osType;
			result.adId = advertisingId;
			result.idfv = SystemInfo.deviceUniqueIdentifier;
			result.userId = AnalyticsUserIdHelper.GetUserId();
			result.sessionId = analyticsSessionManager.SessionInfo.id;
			result.sessionCount = analyticsSessionManager.SessionInfo.count;
			result.appOpenCount = AnalyticsStorageHelper.Instance.GetAppLaunchCount();
			result.userGameCount = AnalyticsStorageHelper.Instance.GetGameCount();
			result.manufacturer = DeviceUtils.Manufacturer;
			result.deviceModel = DeviceUtils.Model;
			result.screenResolution = $"{Screen.width}x{Screen.height}";
			result.appVersion = Application.version;
			result.gameWinRatio = AnalyticsStorageHelper.Instance.GetWinRate().ToString("0.##", CultureInfo.CreateSpecificCulture("en-US"));
			return result;
		}
	}
}
